import 'dart:io';
import '../services/api_service.dart';
import '../models/translate_response.dart';
import 'dart:convert';

class ImageTranslateService {
  final ApiService _apiService;

  ImageTranslateService({ApiService? apiService})
      : _apiService = apiService ?? ApiService();

  /// 翻译图片中的文字
  Future<ImageTranslateResponse> translateImage({
    required File imageFile,
    String sourceLanguage = 'auto',
    String targetLanguage = 'en',
    int pasteType = 1,
  }) async {
    try {
      // 将图片文件转换为Base64
      final bytes = await imageFile.readAsBytes();
      final base64Image = _bytesToBase64(bytes);

      final response = await _apiService.translateImage(
        imageBase64: base64Image,
        from: sourceLanguage,
        to: targetLanguage,
        pasteType: pasteType,
      );

      return response;
    } catch (e) {
      rethrow;
    }
  }
  /// 字节数组转Base64
  String _bytesToBase64(List<int> bytes) {
    return base64Encode(bytes);
  }
}